package com.baor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baor.dao.ChangeDao;
import com.baor.dao.ScheduleDao;
import com.baor.domain.ChangeVo;
import com.baor.domain.ScheduleVo;
import com.baor.service.ChangeService;
import com.baor.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.baor.enums.Results.*;


@Service
public class ChangeServiceImpl implements ChangeService {

    @Autowired
    private ChangeDao changeDao;

    @Autowired
    private ScheduleDao scheduleDao;

    /**
     * 换课审批同意后调用此接口
     * 传入 ChangeVo 实体类
     * @param changeVo
     * @return
     */
    @Override
    public AjaxResult<?> updateData(ChangeVo changeVo) {
        LambdaQueryWrapper<ScheduleVo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ScheduleVo::getClassId,changeVo.getChangeOriginalClassId())
                .eq(ScheduleVo::getTeacherId,changeVo.getChangeOriginalTeacherId())
                .eq(ScheduleVo::getWeekId,changeVo.getChangeOriginalWeekId())
                .eq(ScheduleVo::getCourseId,changeVo.getChangeOriginalWeekId());
        ScheduleVo scheduleVo = scheduleDao.selectList(wrapper).get(0);
        scheduleVo.setWeekId(changeVo.getChangeNewsWeekId());
        scheduleVo.setCourseId(changeVo.getChangeNewsCourseId());
        scheduleVo.setTeacherId(changeVo.getChangeNewsTeacherId());
        scheduleVo.setCourseId(changeVo.getChangeNewsCourseId());
        LambdaQueryWrapper<ScheduleVo> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(ScheduleVo::getClassId,changeVo.getChangeOriginalClassId())
                .eq(ScheduleVo::getTeacherId,changeVo.getChangeOriginalTeacherId())
                .eq(ScheduleVo::getWeekId,changeVo.getChangeOriginalWeekId())
                .eq(ScheduleVo::getCourseId,changeVo.getChangeOriginalWeekId());
        ScheduleVo scheduleVo2 = scheduleDao.selectList(wrapper2).get(0);
        scheduleVo2.setWeekId(changeVo.getChangeNewsWeekId());
        scheduleVo2.setCourseId(changeVo.getChangeNewsCourseId());
        scheduleVo2.setTeacherId(changeVo.getChangeNewsTeacherId());
        scheduleVo2.setCourseId(changeVo.getChangeNewsCourseId());
        scheduleDao.updateById(scheduleVo);
        scheduleDao.updateById(scheduleVo2);
        return AjaxResult.success( UPDATE_SUCCESS );
    }

    @Override
    public AjaxResult<?> list(Map<String,Object> map) {
        if ( map.get("pageIndex") == null )
            map.put("pageIndex",1);
        if ( map.get("pageSize") == null)
            map.put("pageSize",10);
        map.put("pageIndex", (int)map.get("pageSize") * ((int)(map.get("pageIndex")) - 1));
        map.put("pageSize", map.get("pageSize"));
        Map<String,Object> res = new HashMap<>();
        res.put("adjustList",changeDao.list(map));
        res.put("pageCount",changeDao.page(map));
        return AjaxResult.success(res);
    }

    @Override
    public AjaxResult<?> insert(ChangeVo changeVo) {
        LambdaQueryWrapper<ScheduleVo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ScheduleVo::getWeekId,changeVo.getChangeNewsWeekId())
                .eq(ScheduleVo::getCourseId,changeVo.getChangeNewsCourseId())
                .eq(ScheduleVo::getTeacherId,changeVo.getChangeNewsTeacherId())
                .eq(ScheduleVo::getClassId,changeVo.getChangeNewsClassId());
        List<ScheduleVo> scheduleVos = scheduleDao.selectList(wrapper);
        if ( scheduleVos.size() == 0 ){
            return AjaxResult.error("预调课位置不存在课程");
        }
        LambdaQueryWrapper<ScheduleVo> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(ScheduleVo::getWeekId,changeVo.getChangeOriginalWeekId())
                .eq(ScheduleVo::getCourseId,changeVo.getChangeOriginalCourseId())
                .eq(ScheduleVo::getTeacherId,changeVo.getChangeOriginalTeacherId())
                .eq(ScheduleVo::getClassId,changeVo.getChangeOriginalClassId());
        List<ScheduleVo> scheduleVos2 = scheduleDao.selectList(wrapper2);
        if ( scheduleVos2.size() == 0 ){
            return AjaxResult.error("不存在当前此课程");
        }
        changeVo.setChangeStatus("0");
        return AjaxResult.success(changeDao.insert(changeVo) > 0 ? INSERT_SUCCESS : INSERT_ERROR);
    }

    @Override
    public AjaxResult<?> update(ChangeVo changeVo) {
        LambdaQueryWrapper<ScheduleVo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ScheduleVo::getWeekId,changeVo.getChangeNewsWeekId())
                .eq(ScheduleVo::getCourseId,changeVo.getChangeNewsCourseId())
                .eq(ScheduleVo::getTeacherId,changeVo.getChangeNewsTeacherId())
                .eq(ScheduleVo::getClassId,changeVo.getChangeNewsClassId());
        List<ScheduleVo> scheduleVos = scheduleDao.selectList(wrapper);
        if ( scheduleVos.size() == 0 ){
            return AjaxResult.error("预调课位置不存在课程");
        }
        LambdaQueryWrapper<ScheduleVo> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(ScheduleVo::getWeekId,changeVo.getChangeOriginalWeekId())
                .eq(ScheduleVo::getCourseId,changeVo.getChangeOriginalCourseId())
                .eq(ScheduleVo::getTeacherId,changeVo.getChangeOriginalTeacherId())
                .eq(ScheduleVo::getClassId,changeVo.getChangeOriginalClassId());
        List<ScheduleVo> scheduleVos2 = scheduleDao.selectList(wrapper2);
        if ( scheduleVos2.size() == 0 ){
            return AjaxResult.error("不存在当前此课程");
        }
        return AjaxResult.success(changeDao.updateById(changeVo) > 0 ? UPDATE_SUCCESS : UPDATE_ERROR);
    }





}
